home *** CD-ROM | disk | FTP | other *** search
- Path: mn4.swip.net!funsys!funsys.se
- From: rrg@funsys.se (Robin Rosenberg)
- Newsgroups: comp.lang.c++
- Subject: Re: Would/Won't you use a garbage collector?
- Message-ID: <316B6CA4.4F30@funsys.se>
- Date: 10 Apr 96 08:09:08 GMT
- References: <4kamie$e4d@dfw-ixnews3.ix.netcom.com>
- Sender: usenet@funsys.funsys.se
- Organization: Enator Objective Management AB
- X-Mailer: Mozilla 2.0 (Win95; I; 16bit)
- MIME-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
-
- Giuliano Carlini wrote:
- >
- > I'm a long time proponent of using garbage collection in C and C++
- > programs, and I'm curious:
- > - How many others are there?Count One. It was no problem to use it and it was fairly fast. It was
- a small compiler. Version one was a command-line program that only
- allocated memory until it was finished and then the whole program exited.
- The second version was a Windoze version and suddenly memory had to be
- freed between invocations. Using GC was very handy and appropriate.
-
- > - Why don't most C/C++ programmers use it?If you are not 100% sure it will work, you will put your project at
- risk. Secondly your program will use more memory with GC than it
- would otherwise (unless your memory leaks are severe). Alternatively
- you could perform GC very often at the cost of CPU and response time.
-
- > I'm particularly interested in finding out why most C/C++ don't use it.
- > While I have my own theories - which I'll describe below - I'm
- > interested
- > in finding out more directly from those who are against it. I've spent
- > a
- > good part of the past 4 years trying to convince people to use it, and
- > I've got a handle on why the small group of people I associate with are
- > not using it, but I'd like to find out more about why the C/C++
- > community
- > as a whole does not.
-
- Memory is but one resource. The other resources are trickier
- as they often have to be freed in a particular order and conservative
- GC's have their own ideas of when to free resources and their order.
- Secondly if you use global things like notifications (as in MVC) you
- will end up managing your resources explicitly anyway. Weak pointers
- may help there but you have to design for that and the reason for using
- GC was that you didn't want to bother in the first place.
-
- Secondly GC assumes you can take over memory management in the whole
- application. How about shared libraries (DLL's etc). You don't want
- to static linke (even if you could) your whole application.
-
- > I hope that with this information, I'll be able to perfect a more
- > convincing presentation for why the default should be to use garbage
- > collection, and for why explicit calls to free/delete should be used
- > only
- > in the rare cases for which garbage collection is inappropriate.
- >
- > What follows is my belief for why garbage collection is so little used.
- > Feel free to respond to anything I say below, but please, first respond
- > to the questions above. I believe that most people don't use garbage
- > collection because either they:
- > - don't know what it is
- > - don't know it can be used with C/C++.
- > - are misinformation
- > - are biased against it by the C/C++ culture
-
- Probably true on all points. Regarding the last point, one might add that
- GC is not a replacement for proper design, which also solves the memory
- managemnt problem, sometimes at a higher design expense.
-
- > In my experience, most C/C++ programmers either don't know what garbage
- > collection is, or don't know that it can be used with C/C++. After all,
- > no major C/C++ compiler includes a garbage collector. At least, as far
- > as I know. I hope I'm wrong, and that someone can correct me. But even
- > after, I tell them what it is, and that it can be used with C++, almost
- > everyone still rejects it.
-
- One think is that most GC's are free or shareware with little or no support
- and very few programmers are capable of troubleshooting problems related to
- GC. A light can be seen at the other end of the tunnel, though. I saw an ad
- in Object Magazine for a company that claims to have a plug-and-play GC for
- sale.
-
- > At first, most offer technical reasons for rejecting it. Almost all are
- > based on misinformation, since garbage collection is usable and
- > benificial
- > for the vast majority of systems. The most often mentioned is that
- >[.strange jump in article.]
- [...]
- >inventory control. And what else is memory deallocation but a sort of
- >bookkeeping or inventory control. It's just that rather than being for
- >a resource outside of the computer it is for one inside.
-
- You can also use the idea of ownership (has-relations or whatever
- you call it). Make sure you always have an owner to your objects and
- make sure the owner deletes everything it owns. That goes a long
- way towards ridding yourself of memory leaks. auto_ptr and similar
- idioms are also a must.
-
- >For anyone who may be wondering, I believe that we should use garbage
- >collection because:
- > - It vastly decreases the number of bugs in programs which use
- >it.
-
- but it can introduce very subtle bugs. Since GC can happen at "any"
- time it is hard to find out what is the problem when problems occur.
-
- > - It vastly decreases the time to complete programs which use
- >it.
-
- More or less true.
-
- > - It vastly increases the understandability, reuseability,
- > extensability, and maintainability of programs which use
- >it.
-
- Indeed. A lot of code can be removed. One should always consider GC.
-
- >So, do you use garbage collection in your C/C++ programs, and if not,
- >why not?
-
- >giuliano--
- Robin Rosenberg, | Voice: +46-8-7036200 | OO consulting, education
- Enator Objective | Fax: +46-8-7036283 | and mentoring since
- Management AB | Mail: rrg@funsys.se | 1985.
- "Any opinions above are my own *personal* opinions, etc. etc."
-